package DataAccess;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;

import pojo.NguoiDungPOJO;
import pojo.TinNhanPOJO;

public class TinNhanDA extends BaseDA {

	public TinNhanPOJO get(int id) {
		TinNhanPOJO rs = null;
		String sql = "select * from TinNhan where id=" + id;
		provider.open();
		try {
			ResultSet rss = provider.executeQuery(sql);
			while (rss.next()) {
				rs = new TinNhanPOJO();
				rs.setiD(rss.getInt("ID"));
				rs.setIdNguoiGui(rss.getInt("IDNguoiGui"));
				rs.setIdNguoiNhan(rss.getInt("IDNguoiNhan"));
				rs.setNoiDung(rss.getString("NoiDung"));
				rs.setThoiGian(rss.getLong("ThoiGian"));
				rs.setTinhTrang(rss.getInt("TinhTrang"));
				break;
			}
		} catch (Exception e) {
			rs = null;
		}
		provider.close();
		return rs;
	}
	public ArrayList<TinNhanPOJO> getByNguoiNhan(int idNguoiNhan, int tinhTrang){
		ArrayList<TinNhanPOJO> rs=null;
		String sql="select * from TinNhan where IDNguoiNhan="+idNguoiNhan+" and TinhTrang="+tinhTrang;
		provider.open();
		try{
			rs=new ArrayList<TinNhanPOJO>();
			ResultSet rss=provider.executeQuery(sql);
			while(rss.next()){
				TinNhanPOJO tn=new TinNhanPOJO();
				tn.setiD(rss.getInt("ID"));
				tn.setIdNguoiGui(rss.getInt("IDNguoiGui"));
				tn.setIdNguoiNhan(rss.getInt("IDNguoiNhan"));
				tn.setNoiDung(rss.getString("NoiDung"));
				tn.setThoiGian(rss.getLong("ThoiGian"));
				tn.setTinhTrang(rss.getInt("TinhTrang"));
				rs.add(tn);
			}
		}catch (Exception e) {
			rs=null;
		}
		provider.close();
		return rs;
	}
	public ArrayList<TinNhanPOJO> getByNguoiNhan(String tenDangNhap, int tinhTrang){		
		NguoiDungDA da=new NguoiDungDA();
		NguoiDungPOJO nd=da.get(tenDangNhap);
		return getByNguoiNhan(nd.getId(), tinhTrang);
	}
	public int insert(TinNhanPOJO tinNhan){
		provider.open();
		String procName = "spInsertTinNhan";
		String[] input = new String[] { tinNhan.getIdNguoiGui() + "", tinNhan.getIdNguoiNhan() + "",
				tinNhan.getNoiDung() };
		try {
			ResultSet rss = provider.executeStore(procName, input);
			if (rss.next()) {
				return rss.getInt("ID");
			}
		} catch (Exception e) {

		}
		return -1;
	}
	@Override
	protected Object getProperties(ResultSet rss) throws SQLException {
		// TODO Auto-generated method stub
		return null;
	}
}
